package com.caunock.sqlbuilder.adapter.group.impl;

import com.caunock.sqlbuilder.meta.Column;
import com.caunock.sqlbuilder.meta.Index;
import com.caunock.sqlbuilder.meta.Table;
import com.caunock.sqlbuilder.meta.feature.AbstractDataType;
import com.caunock.sqlbuilder.meta.feature.ColumnType;

import java.util.List;

/**
 * SQLite适配器
 *
 * @author Qinweitong
 */
public class SQLiteAdapter extends PostgreSQLAdapter {

    /**
     * 用于记录是否是多主键模式
     */
    private static final ThreadLocal<Boolean> MULTI_PRIMARY_KEY = new ThreadLocal<>();

    @Override
    public StringBuilder body(StringBuilder builder, Table meta) {
        List<Column<?>> columns = meta.getColumns();
        long count = columns.stream().map(Column::isPrimaryKey).filter(item -> item).count();
        // 单个主键在列上定义
        MULTI_PRIMARY_KEY.set(count > 1);
        super.body(builder, meta);
        if (count <= 1) {
            // 移除尾部的换行和逗号
            builder.delete(builder.length() - BR_TAB.length(), builder.length() - 1);
            // 删除最后一个出现的逗号
            int index = builder.lastIndexOf(",");
            builder.delete(index, index + 1);
        }
        // 清除标识
        MULTI_PRIMARY_KEY.remove();
        return builder;
    }

    @Override
    public StringBuilder type(StringBuilder builder, Column<?> column) {
        ColumnType type = column.getType();
        AbstractDataType dataType = type.getDataType();

        switch (dataType) {
            case STRING:
            case CONTENT:
                builder.append(" TEXT");
                break;
            case DECIMAL:
                builder.append(" REAL");
                break;
            case INTEGER:
                builder.append(" INTEGER");
                break;
            case DATE:
                // 以字符串的形式定义日期
                builder.append(" TEXT");
                break;
            case BINARY:
                builder.append(" BLOB");
                break;
            default:
                break;
        }
        return builder;
    }

    @Override
    public StringBuilder nullable(StringBuilder builder, Column<?> meta) {
        boolean autoIncrement = meta.isAutoIncrement();
        if (autoIncrement) {
            return builder;
        }
        return super.nullable(builder, meta);
    }

    @Override
    public StringBuilder increment(StringBuilder builder, Column<?> meta) {
        boolean autoIncrement = meta.isAutoIncrement();
        if (!autoIncrement) {
            return builder;
        }
        // 数值型才能自增
        ColumnType type = meta.getType();
        if (!AbstractDataType.INTEGER.equals(type.getDataType())) {
            return builder;
        }
        // 主键才能自增
        boolean primaryKey = meta.isPrimaryKey();
        if (!primaryKey) {
            return builder;
        }
        boolean multiPrimary = MULTI_PRIMARY_KEY.get();
        if (!multiPrimary) {
            // 单主键需要使用这样的方式定义自增
            builder.append(" PRIMARY KEY");
        }
        return builder;
    }

    @Override
    public StringBuilder primary(StringBuilder builder, Table meta) {
        boolean multiPrimary = MULTI_PRIMARY_KEY.get();
        if (!multiPrimary) {
            // 单主键需要在自增方法中定义
            return builder;
        }
        // 多主键遵从父类规范定义,在尾部定义主键
        return super.primary(builder, meta);
    }

    @Override
    public StringBuilder comment(StringBuilder builder, Table meta) {
        // 不支持额外的注释方式
        return builder;
    }

    @Override
    public StringBuilder comment(StringBuilder builder, Column<?> column) {
        // 不支持额外的注释方式
        return builder;
    }

    @Override
    public StringBuilder tableCommentOn(StringBuilder builder, Table meta) {
        return builder;
    }

    @Override
    public StringBuilder columnCommentOn(StringBuilder builder, Table meta) {
        return builder;
    }

    @Override
    public StringBuilder indexCommentOn(StringBuilder builder, Index meta) {
        return builder;
    }
}
